home *** CD-ROM | disk | FTP | other *** search
/ Programmer Plus 2007 / Programmer-Plus-2007.iso / Programming / Compilers / Miracle C Compiler / miricleC compiler.msi / Instal01.cab / _28417BA49BA2499EBF7281A142A4E49D < prev    next >
Encoding:
Text File  |  2000-08-12  |  3.2 KB  |  109 lines

  1. #include <io.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <assert.h>
  5.  
  6. void xstdio();
  7.  
  8. int main()
  9. {
  10.     xstdio();
  11. }
  12.  
  13. void xstdio()
  14. {
  15.     FILE *fp, tempin, tempout;
  16.     char buf[100];
  17.  
  18.     printf("==> starting xstdio <==\n");
  19.  
  20.     // --- test fgetc on an empty file
  21.     fp = fopen("testdata.txt","w+");    // --- open for r/w and truncate
  22.  
  23.     assert(fp!=NULL && fp>stdprn);
  24.     assert(ferror(fp)==0 && feof(fp)==0);
  25.     assert(fgetc(fp)==EOF);
  26.  
  27.     assert(feof(fp)!=0 && ferror(fp)==0);
  28.     clearerr(fp);
  29.     assert(feof(fp)==0 && ferror(fp)==0);
  30.  
  31.     // --- now we write some bytes to the file
  32.     assert(fwrite("bigboy",3,2,fp)==2);    // --- write 2 elements, each of size 3 bytes
  33.     assert(fputs("badboy",fp)!=EOF);    // --- and another 6 bytes
  34.     assert(fputc('x',fp)=='x'); assert(putc('y',fp)=='y');
  35.     assert(fputc('\n',fp)=='\n'); assert(putc('z',fp)=='z');    // --- for translated file '\n' expands to '\r\n'
  36.     assert(ftell(fp)==17L);
  37.  
  38.     assert(fflush(fp)==0);
  39.     assert(fseek(fp,0L,SEEK_SET)==0);    // --- rewind to the start of file
  40.     assert(ftell(fp)==0L);
  41.     assert(fgets(buf,100,fp)==buf);
  42.     assert(strcmp(buf,"bigboybadboyxy\n")==0);
  43.     assert(fclose(fp)==0);
  44.  
  45.     // --- now we read some bytes from the file
  46.     fp = fopen("testdata.txt","r");
  47.     assert(fgetc(fp)=='b'); assert(fgetc(fp)=='i');
  48.     assert(getc(fp)=='g');  assert(getc(fp)=='b');
  49.     assert(ungetc('m',fp)=='m'); assert(ungetc('n',fp)=='n');
  50.     assert(fgets(buf,100,fp)==buf);
  51.     assert(strcmp(buf,"nmoybadboyxy\n")==0);
  52.  
  53.     assert(ftell(fp)==16L);
  54.     assert(fgets(buf,100,fp)==buf);
  55.     assert(strcmp(buf,"z")==0);
  56.     assert(feof(fp)!=0 && ferror(fp)==0);
  57.     assert(fclose(fp)==0);
  58.  
  59.     // --- now we reassign stdin/stdout and issue standard i/o calls
  60.     memcpy(&tempin,stdin,sizeof(FILE));
  61.     fp=fopen("testdata.txt","r");
  62.     assert(fp!=NULL && fp>stdprn);
  63.     memcpy(stdin,fp,sizeof(FILE));
  64.  
  65.     assert(fread(buf,3,2,stdin)==2);    // --- read in two elements (words) each of size 3 characters
  66.     assert(strncmp(buf,"bigboy",3*2)==0);
  67.  
  68.     assert(getchar()=='b'); assert(getchar()=='a');
  69.     assert(gets(buf)==buf);
  70.     assert(strcmp(buf,"dboyxy")==0);
  71.     assert(gets(buf)==buf);
  72.     assert(strcmp(buf,"z")==0);
  73.  
  74.     // --- note that assert() writes to stdout
  75.     // --- so you should check the "testout.txt" file for assertion failure
  76.     fp=fopen("testout.txt","w");
  77.     assert(fp!=NULL && fp>stdprn);
  78.     memcpy(&tempout,stdout,sizeof(FILE));
  79.     memcpy(stdout,fp,sizeof(FILE));    
  80.  
  81.     strcpy(buf,"first line of testout file\n");
  82.     assert(printf(buf)==strlen(buf));
  83.     assert(putchar('a')=='a'); assert(putchar('b')=='b');
  84.     puts("cde");
  85.     assert(ungetc('j',stdout)==EOF);    // --- cannot ungetc to an output stream
  86.  
  87.     assert(fclose(stdin)==0); assert(fclose(stdout)==0);
  88.  
  89.     // --- return stdin/stdout to previous settings
  90.     memcpy(stdin,&tempin,sizeof(FILE));
  91.     memcpy(stdout,&tempout,sizeof(FILE));
  92.  
  93.     // --- now we read what we wrote to stdout, to make sure it worked
  94.     fp=fopen("testout.txt","r");
  95.     assert(fp!=NULL && fp>stdprn);
  96.     assert(fgets(buf,100,fp)==buf);
  97.     assert(strcmp(buf,"first line of testout file\n")==0);
  98.     assert(fgets(buf,100,fp)==buf);
  99.     assert(strcmp(buf,"abcde\n")==0);
  100.     assert(fgets(buf,100,fp)==NULL);
  101.     assert(feof(fp)!=0 && ferror(fp)==0);
  102.     assert(fclose(fp)==0);
  103.  
  104.     printf("passed stdio tests...\n");
  105.     printf("==> finished xstdio <==\n");
  106.  
  107.     return;
  108. }
  109.